package com.openweixin.sheknows.common;

import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.jboss.logging.Logger;


/**
 * Bean类 - 分页
 * 注意事项：此类只做一般性质的查询，若是复杂查询请单独写方法
 * @author 刘思作
 * @version 1.0.0 2014-04-01
 * @since 1.0.0
 */

public class Pager<T> {

	public static final Logger logger = Logger.getLogger(Pager.class);
	public static final String DIR_DESC = "DESC";// 每页最大记录数限制
	public static final String DIR_ASC = "ASC";// 每页最大记录数限制

	private Integer page = 1;// 当前页码
//	private Integer start = 0;// 从第几条记录开始开始查询
	private Integer limit = 25;// 往后查询多少条
	private Integer totalCount = 0;// 总记录数
	private List<Param> params;// 查询参数
	private String sort = "id";// 排序字段
	private String dir = DIR_DESC;// 排序方式 DESC,ASC
	private List<T> list;// 数据List
	private String keyword;
	private Integer totalPages;//总共多少页

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		if (page < 1) {
			page = 1;
		}
		this.page = page;
	}

	public Integer getStart() {
		return  (Math.max(page-1, 0))*limit;
	}

	
	public Integer getTotalPages() {
		return totalPages;
	}

	//计算总页数
	private void countTotalPages(){
		Double value=Math.ceil(getTotalCount()/limit.doubleValue());
		this.totalPages=value.intValue();
	}
	
	public int getNextPage() {
		return Math.min(page+1, getTotalPages());
	}

	public int getPreviousPage() {
		return Math.max(1,page-1);
	}

	public boolean isHasNextPage() {
		return page<getTotalPages();
	}

	public boolean isHasPreviousPage() {
		return page>1;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
		countTotalPages();
	}

	public List<Param> getParams() {
		if(params == null) {
			params = new LinkedList<Pager.Param>();
		}
		return params;
	}

	public void setParams(List<Param> params) {
		this.params = params;
	}
	
	public Object getParamValueByName(String name) {
		if(params == null) {
			params = new LinkedList<Pager.Param>();
		}
		for (Param param : params) {
			if(param.getName().equals(name)){
				return param.getValue();
			}
		}
		return null;
	}
	/**
	 * 提供key-value形式的添加参数，默认xtype是boolfield，operators是eq
	 * @param name
	 * @param value
	 */
	public void addBoolParam(String key,Boolean value){
		if(key != null && value !=null){
			getParams().add(new Param("boolfield",key,value,"eq"));
		}
	}
	
	/**
	 * 提供key-value形式的添加参数，默认xtype是numberfield，operators是eq
	 * @param name
	 * @param value
	 */
	public void addNumberParam(String key,Number value){
		if(key != null && value !=null){
			getParams().add(new Param("numberfield",key,value,"eq"));
		}
	}
	
	/**
	 * 提供key-value形式的添加参数，默认xtype是textfield，operators是like
	 * @param name
	 * @param value
	 */
	public void addLikeParam(String key,Object value){
		if(key != null && value !=null){
			getParams().add(new Param("textfield",key,value,"like"));
		}
	}
	
	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}



	public static class Param {
		private String xtype = "textfield";
		private String name;
		private Object value;
		private String operators = "like";
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Object getValue() {
			if(xtype.equals("datefield")) {
				try {
					return DateUtils.parseDate(value.toString(), 
							new String[]{"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"});
				} catch (ParseException e) {
					logger.error("无法解析字串为日期类型："+value.toString(),e);
				}
			}else if(xtype.equals("numberfield")){
				return Integer.parseInt(value.toString());
			}else if(xtype.equals("combo") || xtype.equals("combobox")){
				return Integer.parseInt(value.toString());
			}else if(xtype.equals("boolfield")) {
				return Boolean.parseBoolean(value.toString());
			}
			return value;
		}
		public void setValue(Object value) {
			this.value = value;
		}
		public String getOperators() {
			return operators;
		}
		public void setOperators(String operators) {
			this.operators = operators;
		}
		public String getXtype() {
			return xtype;
		}
		public void setXtype(String xtype) {
			this.xtype = xtype;
		}
		
		public Param() {
			super();
		}
		
		/**
		 * 提供的全字段的构造方法
		 * @param xtype
		 * @param name
		 * @param value
		 * @param operators
		 */
		private Param(String xtype, String name, Object value, String operators) {
			this();
			this.xtype = xtype;
			this.name = name;
			this.value = value;
			this.operators = operators;
		}
		
	}
}